#include <iostream>
#include <vector>
template <typename E>
void printVector(const std::vector<E> &v)
{
    for (const E &e : v)
    {
        std::cout << e << " ";
    }
    std::cout << std::endl;
}

template <typename E>
void print(const E &e)
{
    std::cout << e << std::endl;
}

/*

    vector 收缩空间，
        vector 中有capacity 和 size，通常capacity >= size 的

    利用 swap 函数来完成 capacity == size;
*/

int main(int argc, char const *argv[])
{
    std::vector<int> v{};

    for (int i = 0; i < 10000; i++)
    {
        v.push_back(i);
    }

    std::cout << v.front() << std::endl;
    std::cout << "v.size() = " << v.size() << std::endl;
    std::cout << "v.capacity() = " << v.capacity() << std::endl;

    v.resize(10);

    std::cout << "v.size() = " << v.size() << std::endl;
    std::cout << "v.capacity() = " << v.capacity() << std::endl;

    std::vector<int>(v).swap(v); // 匿名对象 std::vector<int>(v) 把v 的吸收了，capacity 和 size相同，又和 v 进行交换，则 v 的 capacity 也和 size 相同了
    std::cout << "v.size() = " << v.size() << std::endl;
    std::cout << "v.capacity() = " << v.capacity() << std::endl;

    for (std::vector<int>::const_iterator i = v.cbegin(); i != v.cend(); i++)
    {
        std::cout << *i << std::endl;
    }

#if 0
// 在这里面代码块中，交换之后直接销毁了，也可以用匿名对象来实现
    {
        std::vector<int> v2 = std::vector<int>(v);

        std::cout << "v2.size() = " << v2.size() << std::endl;
        std::cout << "v2.capacity() = " << v2.capacity() << std::endl;

        v2.swap(v);
        std::cout << "v.size() = " << v.size() << std::endl;
        std::cout << "v.capacity() = " << v.capacity() << std::endl;
        std::cout << "v2.size() = " << v2.size() << std::endl;
        std::cout << "v2.capacity() = " << v2.capacity() << std::endl;
    }
#endif

    return 0;
}